# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   selenium_self
# FileName:      __init__.py
# Author:       test_liu
# Datetime:     2024/10/8 下午8:10
# Description:  日志输入
# ---------------------------------------------------------------------------
import logging
import os
import time


def _log(log_file):
    # 创建log对象
    logger = logging.getLogger()
    # 日志写入级别
    logger.level = logging.INFO
    # 创建日志的headle
    headle = logging.FileHandler(log_file, mode='a', encoding='utf-8')
    # 设置日志的格式
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    # 将格式添加到headle
    headle.setFormatter(formatter)
    # 将headle添加到log对象
    logger.addHandler(headle)
    return logger


# 设置每次执行web自动化时的日志文件名称
time_str = time.strftime(r"%Y-%m-%d %H时%M分%S秒", time.localtime())
log_name = f"{time_str}.log"
# 根据local.ini文件获取日志目录
log_dir = os.path.join(os.path.dirname(__file__), 'report', 'log')
# 如果不存在，则创建一个文件夹
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
log_path = os.path.join(log_dir, log_name)

log = _log(log_path)
